import Dialog from '@vant/weapp/dialog/dialog';
import http from "../../../utils/http";
import {Api} from "../../../utils/api";
Page({
    data: {
        categoryInfo: {
            id: '',
            name: '',
            type: '',
            typeName: ''
        },
        typeList: [
            {id: 0, name: '产品类别'},
            {id: 1, name: '物料类别'},
        ],
    },
    onLoad: function (options) {
        const type = options.type
        const typeList = this.data.typeList
        const typeName = typeList[type].name
        this.setData({'categoryInfo.type': type, 'categoryInfo.typeName': typeName})
    },
    onChangeField(e) {
        const field = e.currentTarget.dataset.field
        const value = e.detail
        this.setData({[`categoryInfo.${field}`]: value})
    },
    onChangeType(e) {
        const typeIndex = e.detail.value
        const typeList = this.data.typeList
        const type = typeList[typeIndex].id
        const typeName = typeList[typeIndex].name
        this.setData({'categoryInfo.type': type, 'categoryInfo.typeName': typeName})
    },
    saveCategory() {
        const categoryInfo = this.data.categoryInfo
        if (categoryInfo.name === '') {
            Dialog.alert({message: '请输入分类名称'})
            return
        }
        http.postRequest(Api.saveCategory, categoryInfo, (res) => {
            Dialog.alert({title: '提示', message: '保存成功'}).then(() => {
                wx.navigateBack()
            })
        })
    }
});
